 -- Inheritance:
---------------
--
--          Gameelement                                   Item (Items are not in the main tree because they are only an logic thing)
--              / \                                         |
--             /   \                                    Item class
--            /     \                                       |
--        Skill    Character                         a particular item
--                     /\          (many more)
--                 Hero  Enemy    /
--                            \-----bear
--                            |   \    
--                      redfish    greenfish
--       Weapon
-- (Container for skills)
 
 
--v.0.2 all characters are now abel to have a item system, all bonus values are now calculated
--v.0.3 i added the equipment slots and wrote a functions function which collects the boni in a table
 
 Gameelement = {
	x = 0, 
	y = 0,
	speed = 0,
	wid = 0, -- for collision area
	hig = 0,
	x_offset = 0, -- offset for collision area
	y_offset = 0,
	z_index = 1, -- on which layer it will be painted
	hp = 100, 
	dir = 'directions[math.random(1,8)]', -- TODO: in radiant ?
	oht = {},
  name = 'a gameelement'}
  
  function Gameelement:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  
  self.dir =' directions[math.random(1,8)]'
  self.x =' math.random(0, SCREEN_WIDTH - self.wid)	'
  self.y ='math.random( 0, SCREEN_HEIGHT - self.hig)'
  return o 
end
   
Character = Gameelement:new{ -- now, Character is just an Instance of Gameelement
hp = 100,
mana = 100, 
speed = 0, 
name = 'a character',
projectiles = {}
}
 
Hero = Character:new{
  	                    str = 10,
	                      agi = 10,
	                      int = 10,
                      	dex = 10,
                        lv  = 1

                    }

 --[[strength  = str 
    inteligence = int
    dexterity = dex
    agility = agi
    level = lv
    attack = atk
    magicattack = matk
    defense = def
    attackspeed = atks
    movementspeed = ms
    ]]

function print_list(list,y)
  if  y == 1 then 
    for i,v in ipairs(list) do 
      print(i,v)
    end
  elseif y == 2 then
    for k,v in pairs(list) do 
      print(k,v)
    end
  end
end

function print_list(list,y)
  if  y == 1 then 
    for i,v in ipairs(list) do 
      print(i,v)
    end
  elseif y == 2 then
    for k,v in pairs(list) do 
      print(k,v)
    end
  end
end

--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------

-- a list of possibel values, which can be ownd by an item


game= {stats= {'str','int','dex','agi','lv'--[['atk','matk','def','atks','ms']]}}

item = {          name = 'a item',
                  typus = 'default',
                  icon = 'a path',
                  picture = 'sth. that changes the chacarcters look',
                  
                  
                  requirement = { 
                            str = 0, -- in the following list the 0 could be replaced by nil to prevent using to much space 
                            int = 0,
                            dex = 0,
                            agi = 0,
                            lv = 0
                                },
                  
                                    
                  bonus = { str = {0,0},
                            int = {0,0},
                            dex = {0,0},
                            agi = {0,0},      
                            lv = {0,0}, -- does not realy increase your lv but allows you to ware items whcih would require i higher lv tha your actual lv
                            --atk = {0,0},
                            --matk = {0,0},
                            --def = {0,0},
                            --atks = {0,0},
                            --ms = {0,0}
                           },                    
                         
                 use = 'a function' -- primaryely this feeld is used by usabal items like potions, but it also can contain a special abilety of an equipment item
                  }
        


----------------------------------------------------------------------
----INVENTORY--------------------------------------------------
----------------------------------------------------------------------



function item:new(o) -- creats a new item class
                  
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  
  setmetatable(o.requirement, self.requirement)
  self.requirement.__index = self.requirement
  
  setmetatable(o.bonus, self.bonus)
  self.bonus.__index = self.bonus
    
  return o

end
        
        
function item:create(o) -- creats a new item
  o = {bonus = {}}
	for k,v in pairs(item.bonus) do --calculats the final bonus
		if self.bonus[k][2] then
 			o.bonus[k] = math.random(self.bonus[k][1],self.bonus[k][2])
		else
			o.bonus[k] = 0
		end
	end
 return self:new(o)
                                                            
  
end


--[[ items are collected in the table item
     helmets: 1001.1 - 2000.1
     armor: 2001.1 - 3000.1
     boots: 3001.1 - 4000.1
     wapons: 4001.1 -5000.1
     if the item is different to the basic item you 
     ]]
     
-- 
 function Character:getItem(_item) -- the hero get an item. this function ( and the needed slot system) shoul be integrated into the class character=> npc and monsters can have items
local _break

for _i = 1,5 do 
  for _ii = 1,24 do 
    if  self.inventory[_i][_ii].item == nil then   self.inventory[_i][_ii].item = _item  _break = 1 break end

    if _i == 5 and _ii ==24 then print('no space left') end
  end
  if _break then break end
end
end

function Character:replaceItem(oldSlot,newSlot)
	
	if  newSlot.item then
		
	else
		newSlot.item = oldSlot.item
		oldSlot.item = nil
		
		
	end

end

function  Character:equipItem(inventorySlot, equipmentSlot)
print"miau"
	for k,v in pairs (inventorySlot.item.requirement) do
	print (k)
		if   "__index" == k then print(ok3)
 			if v <= self[k] then
				if equipmentSlot.item == nil then
			    	if equipmentSlot.typus == inventorySlot.item.typus then
			 		 	self:replaceItem(inventorySlot, equipmentSlot)
					else 
						print ('You can\'t set this item there ')
					end
			  	else 
					print('There is already an item.')
			  	end
			else
				print("you cant use this item")
			end
		end
	end
end

function Character:dropItem(slot)
	o = slot.item
	slot.item = nil
	return o
end


----------------------------------------------------------------------
----------INVENTORY--------------------------------------------------
----------------------------------------------------------------------


function Character:newInventorySlot(pLayer,pRow,pColumn,pPosition) -- this function is used to create the slots for the following iteration. 
                        o = {
                        layer = pLayer,
                        row = pRow,
						column = pColumn,
						position = pPosition,
                        item = nil,
                        amount = 0,
                        --[[position_x = (_ii-1) * 10, -- hard coded, in the end should this be dynamic
                        position_y = (_iii-1) * 10,
                        length_x = 9,
                        length_y = 9 ]]--
                              }
                        return o
                        
end

function Character:createInventory(colums, rows, layers)
self.inventory = {}               		
for i= 1,layers do 
  self.inventory[i] = {}
  for ii=1,colums do 
    for iii= 1,rows do 
      if c then c = c +1 else c = 1 end
      self.inventory[i][c] = self:newInventorySlot(i,ii,iii,c)

    end
  end
c = nil
end

end



 


	


----------------------------------------------------------------------
----------EQUIPMENT--------------------------------------------------
----------------------------------------------------------------------
Character.equipmentSlots = {head ="head",
 							leftHand = "hand",
 							rightHand = "hand",
 							body = "body", 
							hip = "belt", 
							legs = "legs", 
							feets = "feets"}
--possible equipment slots



function Character:newEquipmentSlot(pTypus) -- this function is used to create the slots for the following iteration. 
	o = {
			item = nil,
			typus = pTypus
		}            
return o
                        
end



function Character:createEquipment()

	self.equipment = {}
	for k, v in pairs(self.equipmentSlots)  do  
  	self.equipment[k] = self:newEquipmentSlot(v)
                        
	end

end

function Character:readEquipment()
  local o = {} 
                
  for k, v in pairs(self.equipment) do 
    
    if self.equipment[k].item then 
     	for kk,vv in pairs(self.equipment[k].item.bonus) do
			if  o[kk] then 
      			o[kk] = o[kk] + self.equipment[k].item.bonus[kk]
     		else
				o[kk]= self.equipment[k].item.bonus[kk]
			end
		end
    end
  end
  
  return o
end



                                                     
                                         
             


-- some tests
Hero = Character:new{ str = 10, agi = 10, int = 10, dex = 10,lv  = 1}

item[1001] = item:new {name='dagger',typus = 'hand',requirement = {int = 100}, bonus={str={1,5}, int = {1,10}}} 
schwert = item[1001]:create()
dolch = item[1001]:create()
Hero:createInventory(1,5,5)
Hero:getItem(dolch)
Hero:createEquipment()
Hero:equipItem(Hero.inventory[1][1], Hero.equipment.leftHand)